GameDispatcher = {};
Player = {};
PlayerObserver = {};
Ship = { row : "value", column : "value", length : "value", orientation : "value" };
InitialState = { ships : [Ship] };
GameState = { initialState : InitialState, enemyReady : "value", ownSea : [["value"]],
        enemySea : [["value"]], enemyShips : [Ship] };

GameDispatcher.login = [actorRef(PlayerObserver), "value"];
Player.putInitialState = [InitialState];
Player.fire = ["value", "value"];
PlayerObserver.loginAccepted = [actorRef(Player)];
PlayerObserver.loginRejected = [];
PlayerObserver.initialStateAccepted = [];
PlayerObserver.initialStateRejected = [];
PlayerObserver.enemyStarted = [];
PlayerObserver.gameStarted = [];
PlayerObserver.hit = []
PlayerObserver.fireRejected = [];
PlayerObserver.missed = [];
PlayerObserver.injured = [];
PlayerObserver.killed = [Ship];
PlayerObserver.gameStateChanged = [GameState];

BattleshipClient = actor(function(prefs) {
    this.root = prefs.hostElement;
    this.gameElem = document.createElement("div");
    this.gameElem.className = "game";
    this.root.appendChild(this.gameElem);
    this.ownSeaElem = document.createElement("div");
    this.ownSeaElem.className = "own sea";
    this.gameElem.appendChild(this.ownSeaElem);
    this.enemySeaElem = document.createElement("div");
    this.enemySeaElem.className = "enemy sea";
    this.gameElem.appendChild(this.enemySeaElem);
    this.ownCells = [];
    this.enemyCells = [];
    for (var i = 0; i < 10; ++i) {
        var ownRow = [];
        this.ownCells.push(ownRow);
        var enemyRow = [];
        this.enemyCells.push(enemyRow);
        for (var j = 0; j < 10; ++j) {
            var elem = document.createElement("div");
            elem.className = "cell";
            elem.style.left = j * 32 + "px";
            elem.style.top = i * 32 + "px";
            elem.style.width = "32px";
            elem.style.height = "32px";
            ownRow.push(elem);
            this.ownSeaElem.appendChild(elem);
            var elem = document.createElement("div");
            elem.className = "cell";
            elem.style.left = j * 32 + "px";
            elem.style.top = i * 32 + "px";
            elem.style.width = "32px";
            elem.style.height = "32px";
            enemyRow.push(elem);
            this.enemySeaElem.appendChild(elem);
        }
    }
    this.statusElem = document.createElement("div");
    this.statusElem.className = "statusBar";
    this.root.appendChild(this.statusElem);
    this.hasInitialState = false;
});
BattleshipClient.prototype = new Object();
BattleshipClient.prototype.setStatus = function(text) {
    Util.setText(this.statusElem, text);
}
BattleshipClient.prototype.play = function(game) {
    game.login(this);
    this.setStatus("Регистрация");
}
BattleshipClient.prototype.loginRejected = function() {
    this.setStatus("Неверный адрес игры");
}
BattleshipClient.prototype.loginAccepted = function() {
    this.setStatus("Доступ получен");
}
BattleshipClient.prototype.gameStateChanged = function(state) {
    if (state.initialState != null) {
        var ships = state.initialState.ships;
        for (var i = 0; i < ships.length; ++i) {
            var ship = ships[i];
            this.createShip(this.ownSeaElem, ship);
        }
        this.hasInitialState = true;
    } else {
        this.setStatus("Расставьте корабли");
    }
    if (state.enemyReady) {
        this.enemySeaElem.style.display = "block";
        this.enemySea
    }
}
BattleshipClient.prototype.createShip(seaElem, ship) {
    var shipElem = document.createElement("div");
    shipElement.style.left = ship.row * 32 + "px";
    shipElement.style.top = ship.column * 32 + "px";
    if (ship.orientation == "VERTICAL") {
        shipElement.className = "ship";
        shipElement.style.width = "32px";
        shipElement.style.height = ship.length * 32 + "px";
        for (var i = 0; i < ship.length; ++i) {
            var partElem = document.createElement("div");
            partElem.width = "32px";
            partElem.height = "32px";
            partElem.left = "0px";
            partElem.top = i * 32 + "px";
            if (ship.length == 1) {
                partElem.className = "full";
            } else if (i == 0) {
                partElem.className = "top";
            } else if (i == ship.length - 1) {
                partElem.className = "bottom";
            } else {
                partElem.className = "vert";
            }
            shipElem.appendChild(partElem);
        }
    } else if (ship.orientation == "HORIZONTAL") {
        shipElement.className = "ship";
        shipElement.style.width = "32px";
        shipElement.style.height = ship.length * 32 + "px";
        for (var i = 0; i < ship.length; ++i) {
            partElem.width = "32px";
            partElem.height = "32px";
            partElem.left = i * 32 + "px";
            partElem.top = "0px";
            var partElem = document.createElement("div");
            if (ship.length == 1) {
                partElem.className = "full";
            } else if (i == 0) {
                partElem.className = "left";
            } else if (i == ship.length - 1) {
                partElem.className = "right";
            } else {
                partElem.className = "horz";
            }
            shipElem.appendChild(partElem);
        }
    }
    seaElem.appendChild(shipElem);
}